{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-14T08:42:27.374624Z",
     "start_time": "2024-08-14T08:42:25.030424Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from llama_index.core.tools import FunctionTool\n",
    "from llama_index.core.agent import ReActAgent\n",
    "from llama_index.llms.ollama import Ollama\n",
    "\n",
    "# Define tools\n",
    "def multiply(a: float, b: float) -> float:\n",
    "    \"\"\"Multiply two integers and return the result integer\"\"\"\n",
    "    return a * b\n",
    "\n",
    "# Create FunctionTool instances\n",
    "multiply_tool = FunctionTool.from_defaults(\n",
    "    fn=multiply,\n",
    "    name=\"MultiplyTool\",\n",
    "    description=\"A tool that multiplies two floats.\",\n",
    "    return_direct=True\n",
    ")"
   ],
   "id": "initial_id",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-14T08:42:27.565183Z",
     "start_time": "2024-08-14T08:42:27.375688Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Initialize LLM\n",
    "llm = Ollama(model=\"qwen2:0.5b\", request_timeout=360.0)\n",
    "\n",
    "# Initialize ReAct agent with tools\n",
    "agent = ReActAgent.from_tools([multiply_tool], llm=llm, verbose=True)"
   ],
   "id": "d46caad79b3f9a04",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-14T08:42:33.309189Z",
     "start_time": "2024-08-14T08:42:27.565984Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# direct response\n",
    "res_llm = llm.complete(\"What is 2.3 × 4.8 ? Calculate step by step\")\n",
    "print(res_llm)"
   ],
   "id": "29f3c21096d8331c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "To calculate \\( 2.3 \\times 4.8 \\), you can follow these steps:\n",
      "\n",
      "1. **Perform the multiplication:** When multiplying decimals, simply multiply the numerators (the top numbers) to get the numerator of the product.\n",
      "\n",
      "   \\[\n",
      "   2.3 \\times 4.8 = 9.44\n",
      "   \\]\n",
      "\n",
      "2. **Multiply the denominators (bottom numbers)**\n",
      "\n",
      "   The denominator of \\(4.8\\) is not affected by the multiplication because it does not contain a factor that can affect its value or determine the result.\n",
      "\n",
      "3. **Calculate the product**  \n",
      "   \n",
      "   Since there are no common factors between the numerator and the denominator, the calculation is:\n",
      "\n",
      "   \\[\n",
      "   9.44 = 2.3 \\times 2.3\n",
      "   \\]\n",
      "\n",
      "   This multiplication does not give you a new number because \\(2.3\\) and \\(2.3\\) are already multiplied to get 5.6.\n",
      "\n",
      "So, \\(2.3 \\times 4.8 = 9.44\\).\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-14T08:42:37.068830Z",
     "start_time": "2024-08-14T08:42:33.309189Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# use agent\n",
    "response = agent.chat(\"What is 2.3 × 4.8 ? Calculate step by step\")\n",
    "response.response"
   ],
   "id": "73a424092ac0a89a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Running step 9227846e-d630-4ce2-a760-c8e90366dc6c. Step input: What is 2.3 × 4.8 ? Calculate step by step\n",
      "\u001B[1;3;38;5;200mThought: The task is asking to multiply two numbers, 2.3 and 4.8, then to calculate this multiplication step by step.\n",
      "Action: MultiplyTool\n",
      "Action Input: {'a': 2.3, 'b': 4.8}\n",
      "\u001B[0m\u001B[1;3;34mObservation: 11.04\n",
      "\u001B[0m"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'11.04'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
